package com.saiyaemon.blog.controller.manager;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.saiyaemon.blog.pojo.User;
import com.saiyaemon.blog.service.UserService;
import com.saiyaemon.blog.utils.Constants;


/** 
 * @author  作者 SaiyaEmon
 * @date 创建时间：2017年11月30日 上午12:34:50 
 */
@Controller
@RequestMapping("/manager")
public class LoginController {
	
	private static final Logger LOGGER = LoggerFactory.getLogger(LoginController.class);
	
	@Autowired
	UserService userService;
	
	@RequestMapping(value="/login",method=RequestMethod.GET)
	public ModelAndView showLogin(HttpServletRequest request){
		ModelAndView modelAndView = new ModelAndView("/manager/login");
		Cookie[] cookies = request.getCookies();
		String username ="";
		String password = "";
		String remember = "";
		if(cookies!=null&&cookies.length!=0){
			for (Cookie cookie : cookies) {
				if("username".equals(cookie.getName())){
					username = cookie.getValue();
					modelAndView.addObject("username", username);
				}
				if("password".equals(cookie.getName())){
					password = cookie.getValue();
					modelAndView.addObject("password", password);
				}
				if("remember".equals(cookie.getName())){
					remember = cookie.getValue();
					modelAndView.addObject("remember",remember);					
				}
			}
		}		
		
		return modelAndView;
	}
	
	@RequestMapping(value="/toIndex",method=RequestMethod.GET)
	public ModelAndView login(User user,String remember,HttpServletResponse response,RedirectAttributes attr,HttpSession session){	
		
		ModelAndView modelAndView = new ModelAndView("redirect:/");
		if("".equals(user.getName())||"".equals(user.getPassword())){
			modelAndView.setViewName("/manager/login");
			modelAndView.addObject("error", "用户名和密码不能为空");
		}
		User login = userService.login(user);
		if(login!=null){
			Cookie username = new Cookie("username", user.getName());
			Cookie password = new Cookie("password", user.getPassword());
			username.setMaxAge(Constants.MINUTE_TIME*30);
			password.setMaxAge(Constants.MINUTE_TIME*30);
			response.addCookie(username);
			response.addCookie(password);
			attr.addFlashAttribute("username", user.getName());
			session.setAttribute("username", user.getName());
			if(remember!=null){
				Cookie rememberCookie = new Cookie("remember", "true");
				rememberCookie.setMaxAge(Constants.MINUTE_TIME*30);
				response.addCookie(rememberCookie);
			}
		}else {
			modelAndView.setViewName("/manager/login");
			modelAndView.addObject("error", "用户名或密码不正确");
		}
		return modelAndView;
	}

}
